package cn.jdx.tools.md4img.tools.MdToolsIsLocalFile_T;

public class 相对路径检测 {
    //对相对路径进行检测
    private static String getShortPath(String imgTagContent){
        String lastPath = "";
        if (imgTagContent.contains("/") || imgTagContent.contains("\\")){
            //[1]处理左斜杠
            if (imgTagContent.indexOf("./")==0){
                if(imgTagContent.indexOf(".//")==0){
                    lastPath = imgTagContent.replaceFirst(".//","");
                    System.out.println("[getShortPath]:from("+imgTagContent+")\t=>\tto("+lastPath+")");
                    return lastPath;
                }
                lastPath = imgTagContent.replaceFirst("./","");
                System.out.println("[getShortPath]:from("+imgTagContent+")\t=>\tto("+lastPath+")");
                return lastPath;
            }
            //[2]处理右斜杠
            if (imgTagContent.indexOf(".\\")==0){
                if(imgTagContent.indexOf(".\\\\")==0){
                    lastPath = imgTagContent.replaceFirst(".\\\\\\\\","");
                    System.out.println("[getShortPath]:from("+imgTagContent+")\t=>\tto("+lastPath+")");
                    return lastPath;
                }
                lastPath = imgTagContent.replaceFirst(".\\\\","");
                System.out.println("[getShortPath]:from("+imgTagContent+")\t=>\tto("+lastPath+")");
                return lastPath;
            }
            return imgTagContent;
        }
        //既没有左斜杠，也没有右斜杠
        return imgTagContent;
    }
    public static void main(String[] args) {
        //
        String path11 = "image/saf.gif";
        String path12 = "image\\saf.gif";
        //indexOf("./") == 0
        String path21 = "./image/saf.gif";//(2)
        String path22 = ".//image//saf.gif";//(1)
        //indexOf(".\\") == 0
        String path31 = ".\\image\\saf.gif";//(4)
        String path32 = ".\\\\image\\\\saf.gif";//(3)
        getShortPath(path21);
    }
}
